package com.thinkgem.jeesite.modules.fantasy.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.fantasy.common.EasyUIPage;
import com.thinkgem.jeesite.modules.fantasy.common.PageData;
import com.thinkgem.jeesite.modules.fantasy.entity.City;
import com.thinkgem.jeesite.modules.fantasy.entity.CityMap;
import com.thinkgem.jeesite.modules.fantasy.service.impl.CityMapService;
import com.thinkgem.jeesite.modules.fantasy.service.impl.CityService;

@Controller
@RequestMapping(value = "${adminPath}/fantasy/cityBuild")
public class CityBuildController extends BaseController {
	String message = "success";
	@Autowired
	private CityService cityService;
	@Autowired
	private CityMapService cityMapService;
	
	
	/**
	 * 跳转到城市地点列表页面 add by wanghui 2017.11.19
	 * */
	@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = "/toCityMapList", method = { RequestMethod.GET })
	public ModelAndView toCityMapList(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		List<City> citys = cityService.allCities();
		mv.addObject("citys", citys);
		mv.setViewName("modules/fantasy/citymaplist");
		return mv;
	}

	/**
	 * 获取城市地点列表 add by wanghui 2017.11.19
	 * */
	@ResponseBody
	@RequestMapping(value = "/getCityMapData", method = { RequestMethod.POST }, produces = "text/html;charset=UTF-8")
	public String getCityMapData(HttpServletRequest request) {
		EasyUIPage easyUIPage = getEasyUIAjaxPage();
		PageData pd = new PageData(this.getRequest());
		easyUIPage.setPd(pd);
		List<CityMap> list = cityMapService.queryCityMaplistPage(easyUIPage);
		return this.getEasyUIReturnDataJson(list, easyUIPage.getTotalResult());
	}
	
	
	
	@ModelAttribute
	public CityMap get(@RequestParam(required=false) String cityid) {
		if (StringUtils.isNotBlank(cityid)){
			return cityMapService.get(cityid);
		}else{
			return new CityMap();
		}
	}
	
	@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = {"cityBuild", ""})
	public String cityBuild(CityMap cityMap, HttpServletRequest request, 
			HttpServletResponse response, Model model) {
		logger.debug("cityBuild()");

		List<City> citys = cityService.allCities();
		model.addAttribute("citys", citys);
		return "modules/fantasy/citybuild";
	}

	@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = "mapList")
	public String mapList(String cityid, Model model) {
		logger.debug("mapList()");
		logger.debug("cityid==" + cityid);

		List<City> citys = cityService.allCities();
		model.addAttribute("citys", citys);
		List<CityMap> maps = cityMapService.getCityMapsByCityId(cityid);
		model.addAttribute("maps", maps);
		model.addAttribute("cityid", cityid);
		return "modules/fantasy/maplist";
	}
	
	/**
	 * 编辑或新增地点页面
	 * 
	 * @param CityMap cityMap
	 * @return
	 */
	@RequiresPermissions("fantasy:citybuild:edit")
	@RequestMapping(value = "toEditCityMap", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView toEditCityMap(CityMap cityMap) {
		ModelAndView mv = new ModelAndView();
		logger.debug("mapDetail()");
		if (null == cityMap) {
			mv.setViewName("error/500");
		} else {
			//City city = cityService.get(cityMap.getCity().getId());
			cityMap = cityMapService.getCityMapById(cityMap.getId());
			if (cityMap != null) {//获取到城市id，查询该城市内的地点
				//cityMap.setCity(city);
				//List<CityMap> maps = cityMapService.getCityMapsByCityId(cityMap.getCity().getId());
				List<CityMap> maps = cityMapService.getCityMapsByCityId(cityMap.getCityid());
				mv.addObject("maps", maps);
			} else {//如果没有获取到城市id，需要获取所有地点和所有城市，用来补充信息
				List<CityMap> maps = cityMapService.initMaps();
				List<City> cities = cityService.allCities();
				mv.addObject("cities", cities);
				mv.addObject("maps", maps);
			}
			mv.addObject("curmap", cityMap);
			mv.setViewName("modules/fantasy/editcitymap");
		}
		return mv;
	}

	@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = "toNewCityMap", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView toNewCityMap(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		logger.debug("mapDetail()");
		List<CityMap> maps = cityMapService.initMaps();
		List<City> cities = cityService.allCities();
		mv.addObject("cities", cities);
		mv.addObject("maps", maps);
		mv.setViewName("modules/fantasy/editcitymap");
		return mv;
	}

	/*@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = "mapDetail", method = { RequestMethod.GET })
	public String mapDetail(String cityid, String mapid, Model model) {
		logger.debug("mapDetail()");
		logger.debug("cityid==" + cityid);
		logger.debug("mapid==" + mapid);
		if (mapid != null) {
			CityMap map = cityMapService.getMapByMapId(mapid);
			model.addAttribute("cityid", cityid);
			model.addAttribute("map", map);
		} else {
			model.addAttribute("cityid", cityid);
		}
		List<CityMap> maps = cityService.getMapsByCityId(cityid);
		model.addAttribute("maps", maps);
		return "modules/fantasy/mapbuild";
	}*/

	/*@RequiresPermissions("fantasy:citybuild:view")
	@ResponseBody
	@RequestMapping(value = {"mapBuild", ""})
	public String mapBuild(CityMap cityMap, Model model) {
		logger.debug("mapBuild()");
		logger.debug("cityid==" + cityMap.getCityid());
		int returnValue = -1;
		String result = "builderror";
		if (mapid != null) {
    		CityMap cityMap = cityMapService.getMapByMapId(mapid);
    		cityMap.setPlacename(placename);
    		cityMap.setPlacedesc(placedesc);
    		cityMap.setNorth(north);
    		cityMap.setNortheast(northeast);
    		cityMap.setNorthwest(northwest);
    		cityMap.setWest(west);
    		cityMap.setEast(east);
    		cityMap.setSouth(south);
    		cityMap.setSoutheast(southeast);
    		cityMap.setSouthwest(southwest);
    		Map<String, String> newMap = new HashMap<String, String>();
    		newMap.put("cityid", cityid.toString());
    		newMap.put("north_input", north_input);
    		newMap.put("northwest_input", northwest_input);
    		newMap.put("northeast_input", northeast_input);
    		newMap.put("west_input", west_input);
    		newMap.put("east_input", east_input);
    		newMap.put("southwest_input", southwest_input);
    		newMap.put("south_input", south_input);
    		newMap.put("southeast_input", southeast_input);
    		returnValue = cityMapService.updateMap(cityMap, newMap);
    	} else if (mapid == null) {
    		CityMap cityMap = new CityMap();
    		cityMap.setCityid(cityid);
    		cityMap.setPlacename(placename);
    		cityMap.setPlacedesc(placedesc);
    		if (north != null) {
    			cityMap.setNorth(north);
    		}
    		if (northeast != null) {
    			cityMap.setNortheast(northeast);
    		}
    		if (northwest != null) {
    			cityMap.setNorthwest(northwest);
    		}
    		if (west != null) {
    			cityMap.setWest(west);
    		}
    		if (east != null) {
    			cityMap.setEast(east);
    		}
    		if (south != null) {
    			cityMap.setSouth(south);
    		}
    		if (southeast != null) {
    			cityMap.setSoutheast(southeast);
    		}
    		if (southwest != null) {
    			cityMap.setSouthwest(southwest);
    		}
    		returnValue = cityMapService.mapBuild(cityMap);
		}
		logger.debug("returnValue = " + returnValue);
		if (returnValue > 0) {
			logger.debug("message == buildok");
			result = "buildok";
		}
		
		return result;
	}


	@RequiresPermissions("fantasy:citybuild:view")
	@RequestMapping(value = "form")
	public String form(CityMap cityMap, Model model) {
		model.addAttribute("cityMap", cityMap);
		return "modules/fantasy/mapbuild";
	}*/

	@RequiresPermissions("fantasy:citybuild:edit")
	@RequestMapping(value = "editCityMap")
	@ResponseBody
	public Map<String, Object> editCityMap(CityMap cityMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			/*if (null != cityMap.getId()) {
				cityMap = cityMapService.getCityMapById(cityMap.getId());
			}*/
			Integer result = cityMapService.saveCityMap(cityMap);
			if (result > 0) {
				map.put("result", "创建成功");
				map.put(message, true);
			} else{
				map.put("result", "创建失败");
				map.put(message, false);
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "创建失败，原因："+e.getMessage());
			map.put(message, false);
		}
		return map;
	}
}
